<?php
namespace Admin\Controller;
use Think\Controller;
class UserController extends Controller {
    
    
    public function index()
    {	
    	$users = M('user')->select();
    	$this->assign('users',$users);
    	$this->display('member-list');
    }


    //点击删除管理员
    public function deluser()
    {
    	//var_dump($_POST['delid']);
    	$delid = $_POST['delid'];
    	$sql = "delete from user where id='{$delid}'";
    	$res = M('user')->execute($sql);
    	if ($res) {
    		$arr = array(
    				'error'=>0,
    				'info'=>'已删除!',
    			);
    		echo json_encode($arr);
    		exit;
    	}else{
    		$arr = array(
    				'error'=>1,
    				'info'=>'删除发生错误',
    			);
    		echo json_encode($arr);
    		exit;
    	}
    }

    //修改
    public function show_edit()
    {	
    	//var_dump($_GET['id']);  通过点击修改-->引用layer.open传入url+$_GET路径得到要修改的id  并展示原数据
    	$editID = $_GET['id'];
    	$sql = "select * from user where id='{$editID}'";
    	$edituser = M('user')->query($sql);
    	$this->assign('edituser',$edituser);
    	$this->display('admin-edit');

    }

    //点击保存修改时检测数据 没有错误就uodate数据库
    public function checkedit()
    {
    	//var_dump($_POST);
    	if (empty(I('post.realname'))) {
    		$arr = array(
    				'error'=>1,
    				'info'=>'姓名为必填项',
    			);
    		echo json_encode($arr);
    		exit;
    	}

    	$eid = (I('post.id'));
    	$User = M("user"); // 实例化User对象
		// 要修改的数据对象属性赋值
		$data = (I('post.'));
		$data['password'] = md5(I('post.password'));
		$data['update_time'] = time();
		$res = $User->where('id='.$eid)->save($data); // 根据条件更新记录

		if ($res) {
			$arr = array(
				'error'=>0,
				'info'=>'保存成功!',
			);
			echo json_encode($arr);
			exit;
		}else{
			$arr = array(
				'error'=>1,
				'info'=>'保存中出现错误',
			);
			echo json_encode($arr);
			exit;
		}
    	
		
		
    	
    }

}